/**
 * 
 */
package nl.sportan.samenloop;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author Bram Wiekens
 *
 */
public abstract class ViewPagerActivity extends Activity {
	private ViewPager vp;
	private PagerAdapter vpa;
	private HashMap<Integer,Integer> titles;
	
	
	protected abstract String getTag();
	
	protected abstract PagerAdapter getPagerAdapter();
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		vpa = getPagerAdapter();
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		setContentView(R.layout.samenloop_info);

		titles = createTitleMap();
		
		vp = (ViewPager) findViewById(R.id.pager);
		
		vp.setOnPageChangeListener(createOnPageChangeListener());
		
		vp.setAdapter(vpa);

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);

		initScreenAndButton();
	}

	private void initScreenAndButton() {
		setCurrentTitle(0);
		
		Button next= (Button) findViewById(R.id.next);
		next.setText(getNextTitle(0));
	}
	
	private String getTitle(int i) {
		Integer index = titles.get(i); 
		if (index != null) {
			return getString(index);
		}
		return "";
	}
	
	private String getPrevTitle(int current) {
		if (current > 0) {
			return getTitle(--current);
		}
		return "";
	}
	
	private String getNextTitle(int current) {
		if (current < vpa.getCount() -1) {
			return getTitle(++current);
		}
		
		return "";
	}
	
	private void setCurrentTitle(int current) {
		TextView view = (TextView) findViewById(R.id.screen_title);
		view.setText(getTitle(current));
	}
	
	protected abstract HashMap<Integer,Integer> createTitleMap();
	
	
	private void setButtons(int current){
		Button prev = (Button) findViewById(R.id.prev);
		Button next= (Button) findViewById(R.id.next);
		
		int c= vpa.getCount() -1;
		
		if (current == 0) {
			prev.setVisibility(View.INVISIBLE);
			next.setVisibility(View.VISIBLE);
		}
		
		
		else if (current ==  c) {
			prev.setVisibility(View.VISIBLE);
			next.setVisibility(View.INVISIBLE);
		}
		
		else {
			prev.setVisibility(View.VISIBLE);
			next.setVisibility(View.VISIBLE);
		}
		
		prev.setText(getPrevTitle(current));
		next.setText(getNextTitle(current));
		setCurrentTitle(current);
	}
	
	private OnPageChangeListener createOnPageChangeListener(){
		return new OnPageChangeListener() {
			
			public void onPageSelected(int state) {
				Log.d(getTag(), "on page selected: " +state);
				setButtons(state);
			}
			
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				//Log.d(TAG, "scrolled: " + arg0);
				
			}
			
			public void onPageScrollStateChanged(int arg0) {
				//Log.d(TAG, "selected: " +arg0);
				
			}
		};
		
		
	}
	
	
	protected void onResume(){
		super.onResume();
		
	
	}
}
